/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.login;

import com.tamnhat.entity.Menu;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import telsoft.admin.util.*;
import telsoft.sql.Database;

/**
 *
 * @author Bannt
 */
@ManagedBean
@SessionScoped
public class RuleBean {

    public static Vector listMenu() {
        Connection cn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Vector vtData = new Vector();
        try {
            String strSQL = "";
            cn = AppServer.getConnection();
            strSQL = " select module_name, description from sec_module";
            stmt = cn.prepareStatement(strSQL);
            vtData = Database.convertToVector(stmt.executeQuery());
            return vtData;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
        return null;
    }

    // code by Thangcoder
    public static Vector listRootMenu(Connection cn) throws Exception {
        Statement st = null;                
        ResultSet rs = null;
        Vector vtRootMenu = new Vector();
            String strSQL = "  select * from sec_module where module_type = 'G' and parent_id is null ";
        try {
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                vtRootMenu.addElement(rs.getString(4).toString());
            }
            return vtRootMenu;
        } finally {
            Database.closeObject(st);
            Database.closeObject(rs);
        }
    }

    public static Vector queryChildModuleData(String strUserID, String strFunctionName, Connection cn)
            throws Exception {
        Vector v = new Vector();
        String strSQL = (new StringBuilder()).append(" SELECT sm.module_name,sm.description, smr.right_code,  MOD (MIN (2 * child_level + access_type), 2) access_type  FROM sec_module sm,  (SELECT SUM.module_id, SUM.right_code, SUM.access_type,  0 child_level  FROM sec_user_module SUM, sec_module_right smr  WHERE SUM.module_id = smr.module_id  AND SUM.right_code = smr.right_code  AND SUM.user_id = '").append(strUserID).append("' ").append(" UNION ALL ").append(" SELECT smr.module_id, smr.right_code, smr.access_type, ").append(" 1000 child_level ").append(" FROM sec_module_right smr ").append(" WHERE smr.access_type > 0 ").append(" UNION ALL ").append(" SELECT DISTINCT sgm.module_id, sgm.right_code, sgm.access_type, ").append(" sg.child_level").append(" FROM sec_group_module sgm, ").append(" sec_module_right smr, ").append(" (SELECT     sg.GROUP_ID, LEVEL child_level ").append(" FROM sec_group sg ").append(" WHERE NVL (status, 0) > 0 ").append(" START WITH sg.GROUP_ID IN (SELECT sgu.GROUP_ID ").append(" FROM sec_group_user sgu ").append(" WHERE sgu.user_id = '").append(strUserID).append("') ").append(" CONNECT BY PRIOR sg.parent_group = sg.GROUP_ID) sg ").append(" WHERE sgm.GROUP_ID = sg.GROUP_ID ").append(" AND sgm.module_id = smr.module_id) smr ").append(" WHERE sm.module_id = smr.module_id ").append(" AND NVL (sm.status, 0) > 0 ").append(" AND sm.parent_id = ").append(" (SELECT module_id ").append(" FROM sec_module ").append(" WHERE UPPER (module_name) = UPPER ('").append(strFunctionName).append("')) ").append(" GROUP BY sm.module_name,sm.description, smr.right_code ").append(" HAVING MOD (MIN (2 * child_level + access_type), 2) > 0 ").append(" ORDER BY module_name ").toString();
        v = Database.executeQuery(cn, strSQL);
        return v;
    }
    //ham tao menu su dung cac ham o tren
    public static ArrayList<Menu> createMenu(String userId) throws Exception {
        Connection cn = null;
        ArrayList<Menu> menuList = null;
        try {
            cn = AppServer.getConnection();
            Vector vtRootMenu = new Vector();
            menuList = new ArrayList<Menu>();

            vtRootMenu = listRootMenu(cn);
            if (vtRootMenu.size() > 0) {
                for (int i = 0; i < vtRootMenu.size(); i++) {
                    Menu menu = new Menu();
                    String rootMenu = vtRootMenu.elementAt(i).toString();
                    Vector childMenu = queryChildModuleData(userId, rootMenu, cn);
                    if (childMenu.size() > 0) {
                        menu.setName(rootMenu);
                        ArrayList<Menu> childMenuList = new ArrayList<Menu>();
                        for (int j = 0; j < childMenu.size(); j++) {
                            Vector v = (Vector) childMenu.elementAt(j);
                            Menu child = new Menu();
                            child.setName(v.elementAt(1).toString());
                            child.setUrl(v.elementAt(0).toString());
                            childMenuList.add(child);
                        }
                        menu.setListChild(childMenuList);
                        menuList.add(menu);
                    }
                }
            }
        } finally {
            Database.closeObject(cn);
        }
        return menuList;
    }

    public static Vector getPermissionModule(String userID) throws Exception {
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        Vector vtURLMenu = new Vector();
        String strSQL = "  SELECT sm.module_id,sm.module_type,sm.module_name,sm.description, smr.right_code, MOD (MIN (2 * child_level + access_type), 2) access_type FROM sec_module sm, (SELECT SUM.module_id, SUM.right_code, SUM.access_type, 0 child_level FROM sec_user_module SUM, sec_module_right smr WHERE SUM.module_id = smr.module_id AND SUM.right_code = smr.right_code AND SUM.user_id = '" + userID + "' UNION ALL SELECT smr.module_id, smr.right_code, smr.access_type, 1000 child_level FROM sec_module_right smr WHERE smr.access_type > 0 UNION ALL SELECT DISTINCT sgm.module_id, sgm.right_code, sgm.access_type, sg.child_level FROM sec_group_module sgm, sec_module_right smr, (SELECT sg.GROUP_ID, LEVEL child_level FROM sec_group sg WHERE NVL (status, 0) > 0 START WITH sg.GROUP_ID IN ( SELECT sgu.GROUP_ID FROM sec_group_user sgu WHERE sgu.user_id = '" + userID + "') CONNECT BY PRIOR sg.parent_group = sg.GROUP_ID) sg WHERE sgm.GROUP_ID = sg.GROUP_ID AND sgm.module_id = smr.module_id) smr WHERE sm.module_id = smr.module_id AND nvl(sm.status,0) > 0 GROUP BY sm.module_id,sm.description,sm.module_type,sm.module_name, smr.right_code HAVING MOD (MIN (2 * child_level + access_type), 2) > 0 ";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);

            while (rs.next()) {
                vtURLMenu.addElement(rs.getString(3).toString());
            }
            return vtURLMenu;
        } finally {
            Database.closeObject(cn);
            Database.closeObject(st);
            Database.closeObject(rs);
        }
    }

    public static boolean checkURLPermission(String userID, String pageReq) throws Exception {
        Vector vtListMenu = getPermissionModule(userID);
        for (int i = 0; i < vtListMenu.size(); i++) {
            String path = vtListMenu.elementAt(i).toString();
            String realPath = "/game/tamnhat/" + path;
            if (realPath.equals(pageReq)) {
                return true;
            }
        }
        return false;
    }
}
